package com.kkb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.common.utils.StringUtils;
import com.kkb.domain.AjaxResult;
import com.kkb.page.PageDomain;
import com.kkb.page.TableDataInfo;
import com.kkb.page.TableSupport;
import com.kkb.pojo.TbBrand;
import com.kkb.pojo.TbBusiness;
import com.kkb.service.BrandService;
import com.kkb.service.TbBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.kkb.domain.AjaxResult.error;

@Controller
@RequestMapping("/Brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    //列表
    @GetMapping("/getBrand")
    public String getBrand() {
        return "brand/brandlist";
    }
    //商户分类列表
    @ResponseBody
    @RequestMapping("/getBrandlist")
    public TableDataInfo getCommoditylist() {
        startPage();



        List<TbBrand> tbBrands = brandService.getCommodity();
        for (TbBrand tbBrand : tbBrands) {
            String brandLogo = tbBrand.getBrandLogo();
            if(null!= brandLogo) {
                brandLogo = brandLogo.replaceAll("'", "\\\\'");
            }
            tbBrand.setBrandLogo(brandLogo);
        }



        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(tbBrands);
        rspData.setTotal(new PageInfo(tbBrands).getTotal());
        return rspData;
    }

    /**
     * 设置请求分页数据
     */
    private void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
    }

    @GetMapping("/add")
    public String add() {
        return "brand/add";
    }


    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave( TbBrand tbBrand) {
//        template.setCategoryId(1);
//        template.setIsDelete("0");

        return brandService.insertTbBrand(tbBrand) > 0 ? AjaxResult.success() : error();

    }

    @GetMapping("/delete/{brandId}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("brandId") Integer brandId) {

        return brandService.deletebBrandById(brandId) > 0 ? AjaxResult.success() : error();
    }


    @GetMapping("/edit/{businessId}")
    public String edit(@PathVariable("businessId") Integer brandId, ModelMap map) {

        TbBrand tbBrand = brandService.SelectbBrandById(brandId);
        map.put("tbBrand", tbBrand);
        return "brand/edit";

    }

    @PostMapping("/editSave")
    @ResponseBody
    public AjaxResult editSave(@Validated TbBrand tbBrand) {

        return brandService.updatebBrand(tbBrand) > 0 ? AjaxResult.success() : error();
    }

}
